// pages/article/comment/detail.js
const app=getApp();
var common=require('../../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    replyList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
          title: '数据加载中',
          mask:true,
      });
      var that = this;
      var commentId = options.commentId;
      var token = common.getToken();
      var userId = common.getUserId();
      if(userId==""||userId==null||userId==undefined){
          
          app.toLogin();
          return;
      }
      var par = { commentId: commentId ,userId:userId,token:token};
      wx.request({
          url: app.apiHost + 'article/commentDetail',
          data: par,
          method: 'GET',
          dataType: 'JSON',
          success: function (res) {
              var data = JSON.parse(res.data);
              console.log(data);
              if(data.code==3||data.code==1){     //未登录返回码
                  app.toLogin();       //跳转至登录页面    
                  return;       
              }
              that.setData({
                  replyList: data.content.replyList,
              })
          },
          complete:function(){
              setTimeout(()=>{wx.hideLoading()},500);
          }
      });
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //   发布评论
  sumit_comment: function (e) {
      var is_login = common.checkLogin();
      if (!is_login) return;
      var that = this;
      var comment = e.detail.value.comment;
      var token = common.getToken();
      var userId = common.getUserId();
      var articleId = this.data.articleId;
      var commentId=that.options.commentId;
      var data = {
          userId: userId,
          token: token,
          articleId: 5,
          commentContent: comment,
          commentId:commentId
      };

      var url = `${app.apiHost}article/addcomment`;     //发而评论接口
      var msg = '发布中';
      var func = function (par) {
          let url = `${app.apiHost}article/commentDetail`;  //获取评论 接口
          let data = {
              userId: userId,
              token: token,
              commentId: commentId
          };
          common.request(url, data, '', function (par1) {

              that.setData({ replyList: par1.replyList});
          }, that);
      };
      var res = common.request(url, data, msg, func, that);
  },
})